local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local UIManager = ClientOHOH.load.UIManager();

local OnLineRewardGui=UIManager:CreateUI("OnLineRewardGui",require(script.OnLineRewardGui))
local CommonOHOH = _G.CommonOHOH;
local OnlineReward_Cfg=ClientOHOH.config.OnlineReward;
local Asset_Cfg=ClientOHOH.config.Asset_Cfg;
local ItemTool=_G.CommonOHOH.load.ItemTool();
local HomeGui=UIManager.ui_list.HomeGui;
local CallBackManager = ClientOHOH.load.CallBackManager();
local PetTool=_G.CommonOHOH.load.PetTool();
local ConnectClass=CommonOHOH.load.Connect();
local FunctionList = ClientOHOH.load.FunctionList();
local Client_AudioManager = ClientOHOH.load.Client_AudioManager();
local LanguageHelper=_G.LanguageHelper; 
local EventList = ClientOHOH.load.EventList();
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
function M.new(self,...)
	return M(self,...);
end;
function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.IsCanFreeClaim=false;--是否可以免费一建领取
	OnLineRewardGui:SetRewardCell(#OnlineReward_Cfg);
	self.DataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
    self.Level=self.DataBase:GetData("base").level;
	self.OnLineData=self:CreateData(self.Level);
	OnLineRewardGui:SetData(self.OnLineData);
	self:SetPayBtn();
	 --设置
	 CallBackManager:AddRbxConnect(self,HomeGui.insts.Gift.button.Activated, function()
        OnLineRewardGui:Show()
	end);
	CallBackManager:AddRbxConnect(self,OnLineRewardGui.insts.skip.Activated, function()
        if self.IsCanFreeClaim then
			local ids= self:GetCanClaimReward();
			self:Invoke(FunctionList.online_freeclaim,ids);
		else
            self.parent.RobuxBuyEvent:Action(800)
            Client_AudioManager:PlayGloble("claim");
				-- if self:Invoke(FunctionList.buy_goods,800) then
                --     Client_AudioManager:PlayGloble("claim");
                --     GlobalTipGui:Show(LanguageHelper:GetText(10409));
                -- end
               
                
		end
	end);
	OnLineRewardGui.ClaimAction:Connect(function(id)
        Client_AudioManager:PlayGloble("claim");
        local item_id,count= ItemTool.GetRewardByLevel(OnlineReward_Cfg[id].item_id,OnlineReward_Cfg[id].count,self.DataBase:GetData("base").level);
        local key= ItemTool.GetKeyAndID(item_id)
        if key~="egg" then     --蛋不再单独提示
            ItemTool.FlyingItem(item_id,count);
        end
        
        return self:Invoke(FunctionList.online_claim,id);
    end)
	OnLineRewardGui.ClaimFreeAction:Connect(function()
		self.IsCanFreeClaim=true;
		OnLineRewardGui.insts.skipIcon.Visible=false;
		OnLineRewardGui.insts.skipText.Text=LanguageHelper:GetText(10407);
	end);
	OnLineRewardGui.MinTimeAction:Connect(function(count,timestr)
        HomeGui:UpdateGiftInfo(count,timestr)
    end)
	self.parent.UserDataEvent:Connect(function(data)
        if data["base"] and data["base"].level~=self.Level then
            self.Level=data["base"].level
           for k,v in pairs(self.OnLineData) do
                v:ChangeByLevel(self.Level)
           end
        end
    end)
end;
function  M.SetPayBtn(self)
	--OnLineRewardGui.insts.skipText.Text=LanguageHelper:GetText(10408,{"%%ARG1"},{Good_Cfg[800].price}); 
    OnLineRewardGui.insts.skipText.Text=LanguageHelper:GetText(10408); 
	OnLineRewardGui.insts.skipIcon.Visible=true;
end
function M.GetCanClaimReward(self)
	local ids={};
	for k,v in pairs(self.OnLineData) do
		if(v:IsCanGet())then
			table.insert(ids,k);
		end
	end;
	return ids;
end
function M.CreateData(self,lv)
	local data={}
    for k,v in pairs(OnlineReward_Cfg) do
        local da={
            O_Cfg=v,
            time=v.time,
            claimed=false,
            DataChange=ConnectClass.new(),
            IsCanGet=function(this)
                if this.claimed then
                    return false
                end
    
                if this.time<= 0 then
                    return true
                end

                return false
            end,
            IsClaimed=function(this)
                return this.claimed
            end,
            Claime=function(this)
                this.claimed=true
            end,

            ChangeByLevel=function(this,level)
                if this.O_Cfg.isPet==0 then
                    local itemId,count=ItemTool.GetRewardByLevel(this.O_Cfg.item_id,this.O_Cfg.count,level)
                    this["count"]=count
                    this["icon"]=ItemTool.GetItemShowView(itemId)
                else
                    local itemId,count=ItemTool.GetRewardByLevel(this.O_Cfg.item_id,this.O_Cfg.count,level)
                    this["count"]=count
                    this["model"]=ItemTool.GetItemShowView(itemId)
                    if(this["model"]:IsA("Model"))then
                        local key,id=ItemTool.GetKeyAndID(itemId)
                        this["Cfg"]=PetTool.GetPetDataByPetId(id)
                    end
                   
                end
                this.DataChange:Action()
            end
        }
        da:ChangeByLevel(lv)
        data[k]=da
    end
    return data
end
function M.RestartOnlineReward(self)
	self.IsCanFreeClaim=false;--是否可以免费一建领取
	OnLineRewardGui:SetRewardCell(#OnlineReward_Cfg);
	self.OnLineData={};
	self.OnLineData=self:CreateData(self.Level);
	OnLineRewardGui:SetData(self.OnLineData);
	self:SetPayBtn();
end;
function M.dtor(self)
	M.super.dtor(self);
end;
M:RegistEvent(EventList.restart_reward,M.RestartOnlineReward)
return M;